﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Mbs.ComponentModel.Container;
using Mbs.ComponentModel.Container.Fluent;
using Mbs.ComponentModel.Container.Listner;
using Mbs.Elements;
using Mbs.Globalization;
using Mbs.MessageService;
using Mbs.Messaging;
using Mbs.Test.Model.Gui;
using NUnit.Framework;

namespace Mbs.Test.ElementMap
{

    class CommandNames : Mbs.SystemCommands
    {
        public const string ShowCustomer = "ShowCustomer";
        public const string EnableShowCustomer = "EnableShowCustomer";
        public const string DisableShowCustomer = "DisableShowCustomer";
        public const string HideShowCustomer = "HideShowCustomer";


        public const string ElementMap = "ElementMap";
    }

    interface IElementMapView:IView
    {

    }

    interface IElementMapPresenter : IPresenter<IElementMapView>
    {
    }

    class ElmentMapPresenter : Presenter<IElementMapView>, IElementMapPresenter
    {
        public ElmentMapPresenter()
        {
          
        }

        public override IEnumerator<IDisposable> Subscribe()
        {
            yield return CommandNames.ShowCustomer.Subscribe<EventArgs>((s, e) => MsgBox.ShowMessage("Show Customer"));
            yield return CommandNames.EnableShowCustomer.Subscribe<EventArgs>((s, e) => EnableShowCustomer(true));
            yield return CommandNames.DisableShowCustomer.Subscribe<EventArgs>((s, e) => EnableShowCustomer(false));
            yield return CommandNames.HideShowCustomer.Subscribe<EventArgs>((s, e) => CommandNames.ShowCustomer.Command().Visible = false);
        }

        private static void EnableShowCustomer(bool enabled)
        {
            CommandNames.ShowCustomer.Command().Enabled = enabled;
            CommandNames.EnableShowCustomer.Command().Enabled = !enabled;
            CommandNames.DisableShowCustomer.Command().Enabled = enabled;
        }
    }

    class ElmentMapView :Control, IElementMapView
    {
        public Button btnEnable;
        public Button btnDisable;
        public Button btnHidden;
        public Button btnExit;
        public Button btnShow;


        /// <summary>
        /// 把视图上的UI元素上的相关事件附加到消息总线上
        /// </summary>
        /// <returns></returns>
        public override IEnumerator<IDisposable> Attach()
        {
            yield return CommandNames.ShowCustomer.Attach(btnShow, "Click");
            yield return CommandNames.DisableShowCustomer.Attach(btnDisable, "Click");
            yield return CommandNames.EnableShowCustomer.Attach(btnEnable, "Click");
            yield return CommandNames.HideShowCustomer.Attach(btnHidden, "Click");
            yield return CommandNames.Exit.Attach(btnExit, "Click");
        }

        public override void InitializeResource()
        {
            btnShow.Text = StringParser.Parse("${res:ElementMap.Show}");
            btnDisable.Text = StringParser.Parse("${res:ElementMap.Disable}");

            btnEnable.Text = "ElementMap.Enabled".StringResource();
            btnHidden.Text = "ElementMap.Hide".StringResource();
            btnExit.Text = "Global.Exit".StringResource();
        }

        public override void Init()
        {
            btnDisable = new Button { Id = "btnDisable" };
            btnEnable = new Button { Id = "btnEnable" };
            btnExit = new Button { Id = "btnExit" };
            btnHidden = new Button { Id = "btnHidden" };
            btnShow = new Button { Id = "btnShow" };
        }


        public override string ToString()
        {
            return btnDisable.ToString() + "\r\n" + btnEnable.ToString() + "\r\n" + btnShow.ToString() + "\r\n" + btnHidden.ToString() + "\r\n" + btnExit.ToString() + "\r\n";
        }
    }

    [TestFixture]
    public class ElementMapTest
    {
        IKernel kernel;

        [SetUp]
        public void Init()
        {

            ResourceRepository.StringRegistry.Register("Mbs.Test.Data.Resources", Assembly.GetExecutingAssembly());
           
            kernel = new Kernel();

            kernel.ListnerManager.Register(InitializationListner.Instance);
            kernel.InjectStrategyManager.Register(new Mbs.ComponentModel.Container.Injection.AutoPropertyInjectStragegy());

            kernel.Register(f => f.Bind<IMessageBus>().To<MessageBus>());
            kernel.Register(f => f.Bind<IElementService>().Factory((ctx) => new ElementService()));
            kernel.Register(f => f.Bind<IElementMapView>().Factory((ctx) => new ElmentMapView()));
            kernel.Register(f => f.Bind<IElementMapPresenter>().Factory((ctx) => new ElmentMapPresenter()));
        }

        [Test]
        public void Test()
        {

            var p = kernel.Get<IElementMapPresenter>();
            var view = p.View as ElmentMapView;

            Assert.IsNotNull(p);
            view.Render();

            Assert.IsNotNull(view);


            Console.WriteLine("Click Disable Button");
            view.btnDisable.OnClick();
            view.Render();

            Console.WriteLine("Click Enable Button");
            view.btnEnable.OnClick();
            view.Render();

            Console.WriteLine("Click Exit Button");
            view.btnExit.OnClick();
            view.Render();

            Console.WriteLine("Click Hide Button");
            view.btnHidden.OnClick();
            view.Render();

            Console.WriteLine("Click Show Button");
            view.btnShow.OnClick();
            view.Render();
        }
    }
}
